//
//  LSHTTPClicent.swift
//  新浪微博---01次
//
//  Created by 李宗帅 on 16/5/4.
//  Copyright © 2016年 LS. All rights reserved.
//

import UIKit
import AFNetworking

//怎么定义枚举?
/**
     enum 枚举名: 类型{
        case 情况1 = 类型
        case 情况2 = 类型
     }
*/
enum LSHTTPClientType: String{
    case POST = "POST"
    
    case GET = "GET"
}
class LSHTTPClicent: AFHTTPSessionManager {

    //swift里也有单例,这个单例比较简单
    //static 修饰的变量在静态区
    //存在线程安全问题?
    //swifit中 let本来就设置了线程安全,所以我们在这里不需要考虑线程安全问题
    static let shareInstance: LSHTTPClicent = {
        
        let clicent = LSHTTPClicent()
        
        clicent.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return clicent
    }()
    
    //现在的网络请求就是  get和post
    //网络工具类里创建网络请求的方法
    func request(type: LSHTTPClientType,URLString: String, parameters: AnyObject?, progress downloadProgress: ((NSProgress) -> Void)?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?){
        //把两种请求写到一个方法
        //需要区分一下请求方法,及需要传进一个参数
        if type == .GET{
            //GET
            self.GET(URLString, parameters: parameters, progress: downloadProgress, success: success, failure: failure)
        } else if type == .POST{
            //POST
            self.POST(URLString, parameters: parameters, progress: downloadProgress, success: success, failure: failure)
        }
    }
    
    //抽取出的方法 省去了一个不常用的参数
    func request(type: LSHTTPClientType,URLString: String, parameters: AnyObject?,  success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?){
        
        LSHTTPClicent.shareInstance.request(type, URLString: URLString, parameters: parameters, progress: nil, success: success, failure: failure)
    }
}
